package cn.hxy.bladeframework.dao;

import cn.hxy.bladeframework.model.UserModel;
import com.blade.ioc.annotation.Bean;
import lombok.extern.slf4j.Slf4j;
import org.rex.DB;
import org.rex.db.Ps;
import org.rex.db.exception.DBException;

/** Dao层采用的是rexdb框架
 * @author eric
 * @description:
 * @date 1/4/20 4:31 PM
 */
// TODO 注意这的rexdb与MyBatis可能有点区别，这个不是接口，而是一个类
@Slf4j
@Bean
public class UserDao {

    private final String TABlE_NAME = "user_table";

    /**
     * 添加用户User
     *
     * @param user
     * @return
     */
    public int saveUser(UserModel user) {
        String sql = "INSERT INTO " + TABlE_NAME + "(id, name, age) VALUES (#{id}, #{name}, #{age})";
        int update = 0;
        try {
            update = DB.update(sql, user);
        } catch (DBException e) {
            log.error("{}", e.getMessage(), e);
        }
        return update;
    }

    /**
     * rexdb获取用户User
     *
     * @param id
     * @return
     */
    public UserModel getUser(int id) {
        // 参数
        Ps ps = new Ps();
        ps.add(id);
        String sql = "select * from " + TABlE_NAME + " where id = ?";
        try {
            UserModel userModel = DB.get(sql, ps, UserModel.class);
            return userModel;
        } catch (DBException e) {
            log.error("{}", e.getMessage(), e);
            return null;
        }
    }
}
